const http = require('../../../utils/util')
const {baseUrl}= require('../../../utils/util')
const Base64 = require('../../../utils/base')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        date:"请选择",
        sexList:['男','女'],
        sexIndex:0,
        imgStatus: true,
        user_img: '',
		targetIndex:-1, //目标
		channelIndex:-1,//渠道
		contentIndex:-1, //学习内容
		lengthIndex:-1, //学习时长
		costIndex:-1, //路程
		
		userData:{},
		otherData:{},
		
		//准备参数
		nick_name:'',
		birthday:'请选择', //生日
		sex:'',  //2男 3女
		target_id:'',
		channel_id:'',
		content_id:'',
		length_id:'',
		cost_id:'',
		phone:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		if(options.phone!=0){
			console.log(options.phone);
			this.setData({
				phone:options.phone
			})
		}
		//获取用户资料
		this.getUserData()
    },
	// 上传头像
    userInfoHandler: function () {
      const _this = this
       wx.chooseImage({
		   count: 1,
		   sizeType: ['original', 'compressed'],
		   sourceType: ['album', 'camera'],
           success (res) {
			const tempFilePaths = res.tempFilePaths[0]
			wx.uploadFile({
             url: baseUrl+'/common/imageUpload', //仅为示例，非真实的接口地址
             filePath: tempFilePaths,
             name: 'file',
             success (res){
               let data=JSON.parse(res.data)
			   if(data.status==1){
				   _this.setData({
					   user_img: data.result.all_file_path
				   })
			   }else{
				   wx.showToast({
					   title:'上传失败',
					   icon:'none'
				   })
			   }
             }
           })
         }
       })
  },
  //退出登录
  toLogout(){
	  wx.showModal({
	    title: '提示',
	    content: '确定要退出登录吗？',
	    success (res) {
	      if (res.confirm) {
	        wx.removeStorageSync('code')
	        wx.removeStorageSync('user_id')
	        wx.removeStorageSync('user_token')
			wx.removeStorageSync('phone')
	        wx.navigateBack({
	        		  delta:1
	        })
	      } else if (res.cancel) {
	        console.log('用户点击取消')
	      }
	    }
	  })
  },
  //获取用户资料
  getUserData:function(){
	  const _this= this
	  let data={
		  user_id:wx.getStorageSync('user_id'),
		  user_token:wx.getStorageSync('user_token')
	  }
	  http.post('user/userEditRequest',Base64.encode(JSON.stringify(data))).then(res=>{
		  if(res.status==1){ //成功
		  
			// 性别
		     let sex=''
			 if(res.result.data.sex==1){
				 sex=-1
			 }else if(res.result.data.sex==2){
				 sex=0
			 }else{
				 sex=1
			 }
			 //目标
			 if(res.result.data.target_id){
				 let index=null
				 res.result.other.target.forEach((v,i)=>{
					 if(v.id==res.result.data.target_id){
						 index=i
					 }
				 })
				 this.setData({
					 targetIndex:index
				 })
			 }else{
				 console.log('没有');
			 }
			 // 渠道
			 if(res.result.data.channel_id){
				 let index=null
				 res.result.other.channel.forEach((v,i)=>{
				 	 if(v.id==res.result.data.channel_id){
				 	 index=i
				   }
				 })
				 this.setData({
					channelIndex:index
				 })
			 }else{
				 console.log('没有channel_id');
			 }
			 //学习内容
			 if(res.result.data.content_id){
				let index=null
			 	res.result.other.content.forEach((v,i)=>{
			 		if(v.id==res.result.data.content_id){
						index=i
					}
				})
				this.setData({
					contentIndex:index
				})
			 }else{
			 	console.log('没有content_id');
			 }
			 // 学习时长
			 if(res.result.data.length_id){
				 let index=null
			 		res.result.other.length.forEach((v,i)=>{
			 			if(v.id==res.result.data.length_id){
			 		       index=i
			 		}
			     })
			 	this.setData({
			 		lengthIndex:index
			 	})
			 }else{
			 	console.log('没有length_id');
			 }
			 // 路程
			 if(res.result.data.cost_id){
				let index=null
			 	res.result.other.cost.forEach((v,i)=>{
			 		if(v.id==res.result.data.cost_id){
			 	       index=i
					}
				})
			 	this.setData({
			 		costIndex:index
			 	})
			 }else{
			 	console.log('没有cost_id');
			 }
			 _this.setData({
				 userData:res.result.data,
				 otherData:res.result.other,
				 birthday:res.result.data.birthday,
				 sexIndex:sex,
				 user_img:res.result.data.wechat_avatar
			  })
		  }else{
			  wx.showToast({
			    title: '获取资料失败',
			    icon: 'none'
			  })
		  }
	  })
  },
  //获取手机号
  getPhoneNumber(e){
  	  if(e.detail.errMsg=='getPhoneNumber:ok'){
  	  	wx.login({
  			success:(res)=>{
  				let data={
  					encryptedData:e.detail.encryptedData,
  					code:res.code,
  					iv:e.detail.iv,
  					user_id:wx.getStorageSync('user_id'),
  					user_token:wx.getStorageSync('user_token'),
  				}
  				http.post('user/postPhone',Base64.encode(JSON.stringify(data))).then(res=>{
  					if(res.status==1){
  						wx.setStorageSync('phone',res.result)
						this.setData({
							phone:res.result
						})
  					}else{
  						wx.showToast({
  							title:res.msg,
  							icon:'none'
  						})
  					}
  				})
  			}
  		})
  	  }else{
  	  	wx.showToast({
  	  		title:'授权失败',
  	  		icon:'none'
  	  	})
  	  }
  },
  //保存用户资料
  toSave(){
	  let data={
		  user_id:wx.getStorageSync('user_id'),
		  user_token:wx.getStorageSync('user_token'),
		  wechat_avatar:this.data.user_img,
		  nickname:this.data.nick_name,
		  sex:this.data.sex,
		  birthday:this.data.birthday,
		  target_id:this.data.target_id,
		  channel_id:this.data.channel_id,
		  content_id:this.data.content_id,
		  length_id:this.data.length_id,
		  cost_id:this.data.cost_id,
	  }
	 http.post('user/edit',Base64.encode(JSON.stringify(data))).then((res)=>{
		 if(res.status==1){
			 wx.showToast({
			 	title:'保存成功',
				icon:'none'
			 })
		 }else{
			 wx.showToast({
				 title:'保存失败',
				 icon:'none'
			 })
		 }
	 })
  },
    targetChange: function (e) {
		let selectTarget = this.data.otherData.target[e.detail.value]
		this.setData({
			targetIndex:e.detail.value,
			target_id:selectTarget.id
		},()=>{
			this.toSave()
		})
    },
   
    sexChange: function (e) {
		console.log(e.detail.value);
		let sex=''
		if(e.detail.value==-1){
			sex=1
		}else if(e.detail.value==0){
			sex=2
		}else{
			sex=3
		}
        this.setData({
            sex: sex,
			sexIndex:e.detail.value
        },()=>{
			this.toSave()
		})
    },
    channelChange:function (e) {
		let selectChannel=this.data.otherData.channel[e.detail.value]
        console.log(selectChannel)
		this.setData({
			channelIndex:e.detail.value,
			channel_id:selectChannel.id
		},()=>{
			this.toSave()
		})
      },

      studyChange:function (e) {
		  let selectContent=this.data.otherData.content[e.detail.value]
		  console.log(selectContent)
		  this.setData({
		  	contentIndex:e.detail.value,
		  	content_id:selectContent.id
		  },()=>{
			this.toSave()
		})
        console.log('携带值为', e.detail.value)
       
      },
	  //学习时长
      durationChange:function (e) {
		let selectLength=this.data.otherData.length[e.detail.value]
		  console.log(selectLength)
		  this.setData({
		  	lengthIndex:e.detail.value,
		  	length_id:selectLength.id
		},()=>{
			this.toSave()
		})
      },
	  //路程
      spendChange:function (e) {
        let selectCost=this.data.otherData.cost[e.detail.value]
          console.log(selectCost)
          this.setData({
          	costIndex:e.detail.value,
          	cost_id:selectCost.id
        },()=>{
			this.toSave()
		})
      },
      /*
      生日
       */
    bindDateChange: function (e) {
        console.log('携带值为', e.detail.value)
		this.setData({
			birthday:e.detail.value
		},()=>{
			this.toSave()
		})
      },
		// 昵称修改
	 nicknameBlur(e){
		 this.setData({
			 nick_name:e.detail.value
		 })
	 },	
	


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})